package com.baidu.day05;

import java.util.ArrayList;

public class GoodsService {
    static ArrayList<Goods> goodsList = new ArrayList<>();

    //静态代码块
    static {
        Goods goods1 = new Goods(1,"景元立牌",20.5,100);
        Goods goods2 = new Goods(2,"周日立牌",18.5,50);
        goodsList.add(goods1);
        goodsList.add(goods2);
    }

    /**
     * 查询所有商品
     */
    public void findAll() {
        System.out.println("编号\t商品名称\t商品价格\t商品库存");
        for (Goods goods : goodsList) {
            System.out.println(goods.getNum() + "\t" + goods.getName() + "\t" +
                    goods.getPrice() + "\t" + goods.getStock());
        }
    }

    /**
     * 添加商品
     * @param goods 被添加的商品
     * @return 添加是否成功
     */
    public boolean add(Goods goods) {
       for(Goods g : goodsList) {
           if(g.getName().equals(goods.getName())) {
               System.out.println("该商品已存在,请勿重复添加");
               return false;
           }
       }
        return goodsList.add(goods);
    }

    /**
     * 删除商品
     * @param goodsName 被删除的商品名称
     */
    public void remove(String goodsName) {
        for (int i = 0; i < goodsList.size(); i++) {
            if (goodsList.get(i).getName().equals(goodsName)) {
              goodsList.remove(i);
              System.out.println("删除" + goodsName + "成功");
              return;
            }
        }
        System.out.println(goodsName + "不存在");
    }
}

